package com.luhuiguo.cms.server.handler;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.stereotype.Component;

import com.luhuiguo.cms.server.service.AttachmentService;
import com.luhuiguo.cms.server.util.MapperUtils;
import com.luhuiguo.cms.shared.model.AttachmentModel;
import com.luhuiguo.cms.shared.rpc.LoadAttachments;
import com.luhuiguo.cms.shared.rpc.LoadAttachmentsResult;
import com.luhuiguo.gwt.dispatcher.server.ActionHandler;
import com.luhuiguo.gwt.dispatcher.server.ExecutionContext;
import com.luhuiguo.gwt.dispatcher.shared.exception.ActionException;

@Component
public class LoadAttachmentsHandler implements ActionHandler<LoadAttachments, LoadAttachmentsResult> {

    @Autowired
    private AttachmentService attachmentService;

    @Override
    @PreAuthorize("isAuthenticated()")
    public LoadAttachmentsResult execute(LoadAttachments action, ExecutionContext context) throws ActionException {

        List<AttachmentModel> list = MapperUtils.mapList(attachmentService.findAttachmentByArticleId(action.get()),
                AttachmentModel.class);

        return new LoadAttachmentsResult(list);
    }

    @Override
    public Class<LoadAttachments> getActionType() {
        return LoadAttachments.class;
    }

    @Override
    public void rollback(LoadAttachments action, LoadAttachmentsResult result, ExecutionContext context)
            throws ActionException {

    }

}
